﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Xml;

namespace RTW.Core.Helper
{
    /// <summary>  
    /// Xml的操作帮助类  
    /// </summary> 
    public class XmlHelper
    {
        private string filePath;
        /// <summary>
        /// 路径的参数构造方法
        /// </summary>
        /// <param name="filePath"></param>
        public XmlHelper(string filePath)
        {
            this.filePath = filePath;
        }

        /// <summary>
        /// 创建XML的根节点方法
        /// </summary>
        /// <param name="rootName">根节点名字</param>
        /// <returns></returns>
        public XmlDocument CreateXml(string rootName)
        {
            if (string.IsNullOrEmpty(rootName))
            {
                throw new ArgumentException($"“{nameof(rootName)}”不能是 Null 或为空。", nameof(rootName));
            }

            XmlDocument xml = new XmlDocument();
            //写入声明
            XmlNode node = xml.CreateNode(XmlNodeType.XmlDeclaration, "", filePath);
            xml.AppendChild(node);

            return xml;
        }
        /// <summary>
        /// 新增节点方法
        /// </summary>
        /// <param name="str">添加内容</param>
        /// <returns></returns>
        public XmlDocument AddElement(string str)
        {
            //加载指定的XML文件
            XmlDocument xml = new XmlDocument();
            xml.Load(filePath);
            var root = xml.DocumentElement;//取到根结点
            XmlNode node = xml.CreateElement("element", "New", "");
            node.InnerText = str;

            //添加为根元素的第一层子结点
            root.AppendChild(node);

            return xml;
        }
        /// <summary>
        /// 删除节点方法 
        /// </summary>
        /// <param name="str">删除指定元素</param>
        /// <returns>true成功</returns>
        public bool DeleteDocument(string str)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(filePath);
            try
            {
                XmlElement node = (XmlElement)xml.SelectSingleNode("");
                node.RemoveAttribute(str);//删除指定元素
            }
            catch (Exception)
            {

                return false;
            }
            return true;
        }
        /// <summary>
        /// 保存XML文件方法
        /// </summary>
        /// <param name="xml">xml</param>
        /// <param name="filePath">保存路径</param>
        public void SaveXml(XmlDocument xml, string filePath)
        {
            xml.Save(filePath);
        }
        /// <summary>
        /// 创建根节点对象方法
        /// </summary>
        /// <param name="xml"></param>
        /// <param name="str">根节点名字</param>
        /// <returns></returns>
        public XmlDocument AddElement(XmlDocument xml, string str)
        {
            //增加根节点
            XmlElement rootElement = xml.CreateElement(str);
            xml.AppendChild(rootElement);
            return xml;
        }
        /// <summary>
        /// 获取指定XPath表达式节点的值方法
        /// </summary>
        /// <param name="xPath">XPath表达式</param>
        /// <returns></returns>
        public string GetValue(string xPath)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(filePath);
            XmlElement element = xml.DocumentElement;
            return element.SelectSingleNode(xPath).InnerText;
        }
        /// <summary>
        /// 获取指定XPath表达式节点的属性值方法
        /// </summary>
        /// <param name="xPath">XPath表达式</param>
        /// <param name="attributeName">属性名</param>
        /// <returns></returns>
        public string GetAttributeValue(string xPath, string attributeName)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(FilePath);
            //创建根对象
            XmlElement element = xml.DocumentElement;

            //返回XPath节点的属性值
            return element.SelectSingleNode(xPath).Attributes[attributeName].Value;
        }
        /// <summary>
        /// 获取指定XPath表达式的节点对象方法
        /// </summary>
        /// <param name="xPath">XPath表达式</param>
        /// <returns></returns>
        public XmlNode GetNode(string xPath)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(FilePath);
            //创建根对象
            XmlElement element = xml.DocumentElement;
            return element.SelectSingleNode(xPath);
        }
        public string FilePath { get => filePath; set => filePath = value; }
    }
}
